package com.meals.system.service.impl;

import com.meals.system.api.system.vo.WhitesMealIndexConfigGoodsVO;
import com.meals.system.common.ServiceResultEnum;
import com.meals.system.dao.IndexConfigMapper;
import com.meals.system.dao.WhitesMealGoodsMapper;
import com.meals.system.entity.IndexConfig;
import com.meals.system.entity.WhitesMealGoods;
import com.meals.system.service.WhitesMealIndexConfigService;
import com.meals.system.util.BeanUtil;
import com.meals.system.util.PageQueryUtil;
import com.meals.system.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Description:
 * Parm:
 * return:
 * Author: Chen
 * Date: 2021/12/4
 */
@Service
public class WhitesMealIndexConfigServiceImpl implements WhitesMealIndexConfigService {

    @Autowired
    private IndexConfigMapper indexConfigMapper;

    @Autowired
    private WhitesMealGoodsMapper whitesMealGoodsMapper;




    @Override
    public List<WhitesMealIndexConfigGoodsVO> getConfigGoodsesForIndex(int configType, int number){
        List<WhitesMealIndexConfigGoodsVO> whitesMealIndexConfigGoodsVOS = new ArrayList<>(number);
        List<IndexConfig> indexConfigs = indexConfigMapper.findIndexConfigsByTypeAndNum(configType, number);
        if(!CollectionUtils.isEmpty(indexConfigs)){
            // 取出所有的goodsId
            List<Long> goodsIds = indexConfigs.stream().map(IndexConfig::getGoodsId).collect(Collectors.toList());

            List<WhitesMealGoods> whitesMealGoods = whitesMealGoodsMapper.selectByPrimaryKeys(goodsIds);
            whitesMealIndexConfigGoodsVOS = BeanUtil.copyList(whitesMealGoods, WhitesMealIndexConfigGoodsVO.class);
            for (WhitesMealIndexConfigGoodsVO whitesMealIndexConfigGoodsVO : whitesMealIndexConfigGoodsVOS){
                String goodsName = whitesMealIndexConfigGoodsVO.getGoodsName();
                String goodsIntro = whitesMealIndexConfigGoodsVO.getGoodsIntro();
                // 字符串过长导致文字炒出问题
                if (goodsName.length() > 30){
                    goodsName = goodsName.substring(0, 30) + "...";
                    whitesMealIndexConfigGoodsVO.setGoodsName(goodsName);
                }
                if (goodsIntro.length() > 22){
                    goodsIntro = goodsIntro.substring(0,22) + "...";
                    whitesMealIndexConfigGoodsVO.setGoodsIntro(goodsIntro);
                }
            }
        }
        return whitesMealIndexConfigGoodsVOS;
    }

    @Override
    public PageResult getConfigsPage(PageQueryUtil pageUtil){
        List<IndexConfig> indexConfigs = indexConfigMapper.findIndexConfigList(pageUtil);
        int total = indexConfigMapper.getTotalIndexConfigs(pageUtil);
        PageResult pageResult = new PageResult(indexConfigs, total, pageUtil.getLimit(), pageUtil.getPage());
        return pageResult;
    }

    @Override
    public String saveIndexConfig(IndexConfig indexConfig){
        if (whitesMealGoodsMapper.selectByPrimaryKey(indexConfig.getGoodsId())==null){
            return ServiceResultEnum.GOODS_NOT_EXIST.getResult();
        }
        if (indexConfigMapper.selectByTypeAndGoodsId(indexConfig.getConfigType(), indexConfig.getGoodsId()) != null){
            return ServiceResultEnum.SAME_LOGIN_NAME_EXIST.getResult();
        }
        if (indexConfigMapper.insertSelective(indexConfig) > 0){
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public String updateIndexConfig(IndexConfig indexConfig){
        if (whitesMealGoodsMapper.selectByPrimaryKey(indexConfig.getGoodsId()) == null){
            return ServiceResultEnum.GOODS_NOT_EXIST.getResult();
        }
        IndexConfig temp = indexConfigMapper.selectByPrimaryKey(indexConfig.getConfigId());
        if (temp == null){
            return ServiceResultEnum.DATA_NOT_EXIST.getResult();
        }
        IndexConfig temp2 = indexConfigMapper.selectByTypeAndGoodsId(indexConfig.getConfigType(), indexConfig.getGoodsId());
        if (temp2 != null && !temp.getConfigId().equals(indexConfig.getConfigId())){
            // goodsId相同且不同id不能继续修改
            return ServiceResultEnum.SAME_INDEX_CONFIG_EXIST.getResult();
        }
        indexConfig.setUpdateTime(new Date());
        if (indexConfigMapper.updateByPrimaryKeySelective(indexConfig) > 0){
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }


    @Override
    public IndexConfig getIndexConfigById(Long id) {
        return indexConfigMapper.selectByPrimaryKey(id);
    }

    @Override
    public Boolean deleteBatch(Long[] ids){
        if (ids.length < 1){
            return false;
        }
        // 删除数据
        return indexConfigMapper.deleteBatch(ids) > 0;
    }
}
